package com.xhj.controller;

import com.xhj.service.PayLogService;
import com.xhj.util.ResultEntity;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 支付日志表(TPayLog)表控制层
 *
 * @author makejava
 * @since 2021-03-11 13:22:24
 */
@CrossOrigin
@RestController
@RequestMapping("/order/payLog/")
public class PayLogController {

    @Autowired
    private PayLogService payLogService;

    @ApiOperation(value = "生成支付二维码")
    @RequestMapping("createNative/{orderNo}")
    public ResultEntity createNative(@PathVariable String orderNo){
        Map<String,Object> map = payLogService.createNative(orderNo);
        return ResultEntity.successWithData().data("map",map);
    }

    @ApiOperation(value = "查询支付状态")
    @RequestMapping("queryPayStatus/{orderNo}")
    public ResultEntity queryPayStatus(@PathVariable String orderNo){
        Map<String, String> resultMap = payLogService.queryPayStatus(orderNo);
        if (resultMap == null) return ResultEntity.failedWithMessage("支付出错！");
        // 支付成功
        if (resultMap.get("trade_state").equals("SUCCESS")){
            // 更新订单支付状态
            payLogService.updatePayStatus(resultMap);
            return ResultEntity.successWithoutData();
        }
        // 返回支付中的状态
        return ResultEntity.successWithoutData().setCode(25000);
    }
}